#!/usr/bin/python3

import re
import json
import requests

# Headers: simulate wechat embedded browser
HEADERS = {
    'Connection': 'keep-alive',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)'
                  ' Chrome/39.0.2171.95 Safari/537.36 MicroMessenger/6.5.2.501'
                  ' NetType/WIFI WindowsWechat QBCore/3.43.27.400 QQBrowser/9.0.2524.400',
    'Accept-Encoding': 'gzip, deflate'
}
COOKIES = {
    'zb':{
    'ASP.NET_SessionId': 'o2ctpljg5whideb2fy1qjhox',
    '.ASPXAUTH':'71566DABE226D6D4180C8F5CAD5F1AC0B77AC632E2DEC4454884761DB7EB4130178BD66487161E5F819C7B2DEDFD328ED69FC23DED7F4A0B5B9CAF86419B6AE34962C371E888071A5A2628EA88DFCFBC3ED4F65E0FB763B6CD29CE97B4F9BABF716635AA4826D4E2EE6F586DAB54E5A0D337B5F1CAC8B5EAA43F1134911B0947749432FF84574B89565F02BAF36198CC'
    },
    'zn':{
    'ASP.NET_SessionId': 'o2ctpljg5whideb2fy1qjhox',
    '.ASPXAUTH':'C5D2DFC48CAA3EB3E5CD9C1D68E9554D623135B6404BC73F38A655CEF361D6100E694201675A1938C2B26C16F022EDD95574FFBFF2D7260E569C7718BCAE2F9542BCBDF257B5BE20F93DF8AF86ABB0379EAAD274035829F8D628CE9CD55865606651ECBC615DDF4A382F478FF79949F7AE0BB5FBD0CADA11E102875677FF1C2CF33B84A93359C7836529730CB7926931'
    },
    'hh':{
    'ASP.NET_SessionId': 'o2ctpljg5whideb2fy1qjhox',
    '.ASPXAUTH':'B6AF9FC45C57121DD9E2E774A3154A3A96FF0F3B932CFB40B4EDA90DE54B0CFD51FF021610235B9E29C771086A5ED6EB4111E3CE2CA8CD0D5E4B05A831BC92D2FFFC9EF78CB1B2BC263348028A9DA658FC26612FC0CAE0F0E8AC4A13909B7B19B9A561137A6960AEF3DFD3AC16844D5100450D9D4C11ADEE9FB739531F5F3E7249C51D8F61DCB3A1C33E814F76E93A4C'
    },
    'jw':{
    'ASP.NET_SessionId': 'o2ctpljg5whideb2fy1qjhox',
    '.ASPXAUTH':'10EF427D67221E0135E21EC2087B2057CB862859CD9262CF190BFF0A514E7A4AF15099BE9F8C8C9192B38900FE61791196126053C2AD042B8BA4E98C5666F69DCC6ACB7F44C8B84C984A982EB7F60C8823B6863B5E901677D82E233065BA9DD7A73D121321522D55BC5508A5C5DF9A3E4D2C6EDFE0636AAF6CA4C4560C456B4A'
    },
    'zx':{
    'ASP.NET_SessionId': 'o2ctpljg5whideb2fy1qjhox',
    '.ASPXAUTH':'F320B78D0C623959134310C1262A456870A1972878D9FE35D51B6D7BD433D0BF6D3641BCC59826D87744C8C13775633D49AA76E76AD89E442E3AF8C06274189D0A76344059284BC57731F386EBE5DDD066C42B6B94E1203DF166A34E729C9972295E1157A26AF2AAE8C4411B99EF1D5C563A9226890EA47A2EF3B9B347533869'
    }
}

REQUEST_URL = 'http://58.215.207.22:8078/Realtime/Wastewater.aspx'

def get_info(key):
    """Get data according to openid"""
    data = requests.get(url=REQUEST_URL, cookies = COOKIES['jw'], headers=HEADERS)
    data.encoding = "utf-8"
    try:
        return data.text
    except LookupError:
        return {"Error": "Cookies could be out of date"}


def parse_code(code):
    """Parse web page source code"""
    match_strings = re.findall(r"window.json = (\S+);", code)
    # json strings are hided in js code
    if len(match_strings) == 0:
        raise LookupError
    else:
        json_string = match_strings[0]
    json_object = json.loads(json_string)
    data_dict = {
        "openid": json_object.get("myopenid"),
        "Today Steps": json_object.get("rankdesc").get("score"),
        "History Steps": json_object.get("rankdetaillist")[0].get("score"),
        "Highest Record": json_object.get("rankdetaillist")[1].get("score")
    }
    return data_dict


if __name__ == "__main__":
    print(get_info("o95gOt6gGIfTROCYQajbVCfFr9BA"))